<?php
namespace Admin\Controller;
use think\controller;
use Think\Page;

class UserController extends AuthController{

    public function index(){
        $this->page_title="ئەزالار تىزىملىكى";
        $this->sessions=M('blog_admin')->where(array('aid'=>$_SESSION['name']))->find();
        $this->users=M('blog_admin')->select();
        $this->display();
    }

    public function add()
    {
        $this->page_title=" باشقۇرغۇچى قوشۇش";
        $this->display();
    }

    //提交表单
    public function addhend()
    {
        $post=I('post.');
        $post['intime']=time();
        //1.判断为空
        $auser=$post['auser'];
        $apass=$post['apass'];
        if($apass==''||$auser==''){
            $this->error('用户名或密码不能为空！');
        }
        //2.添加到数据库
        if(!M('blog_admin')->add($post)) $this->error('添加失败！');
        $this->success('用户添加成功了',U('User/index'),1);
    }
    //编辑管理员信息
    public function edit($aid)
    {
        $this->page_title="ئەزا تەھرىرلەش";
        $this->users=M('blog_admin')->where(array('aid'=>$aid))->field('aid,auser,apass,status,email')->find();
        $this->display();
    }
    //提交表单
    public function savehend()
    {
        $post=I('post.');
        if(!M('blog_admin')->save($post)) $this->error('编辑失败！');
        $this->success('编辑成功',U('User/index'),1);
    }

    //删除管理员
    public function delete($aid)
    {
        if(M('blog_admin')->where(array('aid'=>$aid))->delete()){
            $data= ['status'=>1,'message'=>'ئۆچۈرۈش تامام'];
        }
        else{
            $data= ['status'=>0,'message'=>'ئۆچۈرۈش مەغلۇب بولدى!'];
        }
        $this->ajaxReturn($data);
    }
}